<?php
/**
 * iview image-input组件
 */
namespace Controller;
use Kuyuan\WqCore\controller\Controller;
use Model\W7CoreAttachmentModel;

class ImageInput extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 通用删除文件
     * @param string $_path 文件路径
     * @return boolean
     */
    public function remove($_path)
    {
        load()->func('file');
        file_delete($_path);
        file_remote_delete($_path);
        return true;
    }

    /**
     * 获取图片的真是地址src
     * @param path 文件目录路径
     * @return string 返回完整的文件链接
     */
    public function getSrc($_path)
    {
        return tomedia($_path);
    }

    /**
     * 获取当前公众号系统图片列表总数
     */
    public function getImgTotal()
    {
        return abs(W7CoreAttachmentModel::where("uniacid",$this->_uniacid)
                            ->where("type",1)
                            ->count());
    }

    /**
     * 获取当前公众号系统图片列表
     * @param array $_limit 请求下标
     * @return mixed
     */
    public function getImgList($_limit)
    {
        return W7CoreAttachmentModel::where("uniacid",$this->_uniacid)
                                        ->where("type",1)
                                        ->limit($_limit[0],$_limit[1])
                                        ->order('createtime','desc')
                                        ->field(array('id','filename','attachment'))
                                        ->select();
    }

    /**
     * 上传图片至公众号系统
     * @param FILES['file']  上传的文件
     * @return boolean
     */
    public function uploadImg()
    {
        load()->func('file');
        $_result = file_upload($_FILES['file'],'image');
        if($_result['success']){
            if($this->_w['setting']['remote']['type'] != 0){
                file_remote_upload($_result['path']);
            }
            if($this->uploadImgRecord($_result['path'])){
                return true;
            }
        } else {
            return false;
        }
    }

    /**
     * 数据库新增上传图片记录
     * @param $_attachment 上传的图片路径
     * @return Boolen 是否新增成功
     */
    private function uploadImgRecord($_attachment)
    {
        return W7CoreAttachmentModel::create(
            array(
                'uniacid'=>$this->_uniacid,
                'uid'=>1,
                'filename'=>$_FILES['file']['name'],
                'attachment'=>$_attachment,
                'type'=>1,
                'createtime'=>TIMESTAMP,
                'group_id'=>-1
            )
        );
    }

    /**
     * 删除微擎系统图片
     * @param int $_attachment_id  删除的id
     * @return boolean 删除一个文件
     */
    public function deleteImg($_attachment_id)
    {
        load()->func('file');
        $Attachment = W7CoreAttachmentModel::where("id",$_attachment_id);
        $_attachment = $Attachment->value("attachment");
        if($_attachment) {
            if (is_file(ATTACHMENT_ROOT . $_attachment)) {
                $_result = file_delete($_attachment);
            } else {
                $_result = file_remote_delete($_attachment);
            }
            if($_result){
                return W7CoreAttachmentModel::delete($_attachment_id,true);
            }else{
                return false;
            }
        }
    }
}